// eslint-disable-next-line import/prefer-default-export
export const getJsonDiff = (obj1: any, obj2: any) => {
  if (typeof obj1 !== 'object' || typeof obj2 !== 'object') {
    return obj1 !== obj2 ? obj1 : undefined;
  }
  const diff: Record<string, any> = {};
  // eslint-disable-next-line no-restricted-syntax
  for (const key in obj1) {
    // eslint-disable-next-line no-prototype-builtins
    if (obj1.hasOwnProperty(key)) {
      if (obj2[key] === undefined) {
        diff[key] = obj1[key];
      } else {
        const nestedDiff = getJsonDiff(obj1[key], obj2[key]);
        if (nestedDiff !== undefined) {
          diff[key] = nestedDiff;
        }
      }
    }
  }

  // eslint-disable-next-line no-restricted-syntax
  for (const key in obj2) {
    // eslint-disable-next-line no-prototype-builtins
    if (obj2.hasOwnProperty(key) && obj1[key] === undefined) {
      diff[key] = obj2[key];
    }
  }
  return Object.keys(diff).length > 0 ? diff : undefined;
};
